package com.kafka.controller;

import com.alibaba.fastjson.JSONObject;
import com.kafka.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.kafka.support.SendResult;
import org.springframework.util.concurrent.ListenableFuture;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping(value = "/producer")
public class KafkaProducerController {

    @Autowired
    KafkaTemplate<String,Object> kafkaTemplate;

    @RequestMapping(value = "/users")
    public String postMessage(){
        User user = new User();
        user.setAge(10);
        user.setId(1000000L);
        user.setUserName("名字");
        user.setWeighter(true);
        kafkaTemplate.send("user_topic_test", JSONObject.toJSONString(user));
        return "成功";
    }

}
